package com.jic.product.controller.manage;


import com.jic.common.base.vo.RestResult;
import com.jic.conf.api.manage.SysProvinceCityApi;
import com.jic.conf.response.RegionResponse;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.ProductSaleAreaRelation;
import com.jic.product.manage.request.ProductSaleAreaEditRequest;
import com.jic.product.manage.request.ProductSaleAreaRelationRequest;
import com.jic.product.manage.response.ProductSaleAreaRelationResponse;
import com.jic.product.service.ProductSaleAreaRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZXK on 2020/1/9 11:04
 * 商品销售区域管理
 */
@RestController
@RequestMapping("productSaleAreaRelation")
@Api(value = "商品销售区域", description = "商品销售区域", tags = "商品销售区域")
public class ProductSaleAreaRelationController {

    @Autowired
    private ProductSaleAreaRelationService productSaleAreaRelationService;

    @Resource
    SysProvinceCityApi sysProvinceCityApi;

    /*
     *  商品已保存区域
     *
     * */
    @ResponseBody
    @GetMapping(value = "/queryProductSaleAreaRelation")
    public RestResult queryByProductId(Long productId) {
        RestResult restResult = new RestResult();
       List<ProductSaleAreaRelationResponse>  productSaleAreaRelationResponses=new ArrayList<>();

        try {
            List<ProductSaleAreaRelation> saleAreaInfo = productSaleAreaRelationService.queryByProductId(productId);
            if (saleAreaInfo.size() > 0) {
                List<Long> saleAreaIds=new ArrayList<>();
                for(ProductSaleAreaRelation productSaleAreaRelation:saleAreaInfo){
                    saleAreaIds.add(productSaleAreaRelation.getSaleAreaId());
                }
                RestResult<List<RegionResponse>> saleAreaList = sysProvinceCityApi.getRegionListByIds(saleAreaIds);
                int i=0;
                for(RegionResponse regionResponse:saleAreaList.getData()){
                    ProductSaleAreaRelationResponse productSaleAreaRelationResponse=new ProductSaleAreaRelationResponse();
                    productSaleAreaRelationResponse.setAreaCode(regionResponse.getAreaCode());
                    productSaleAreaRelationResponse.setMerchantCode(regionResponse.getMerchantCode());
                    productSaleAreaRelationResponse.setAreaName(regionResponse.getAreaName());
                    productSaleAreaRelationResponse.setSaleAreaRelationId(saleAreaInfo.get(i).getId());
                    productSaleAreaRelationResponses.add(productSaleAreaRelationResponse);
                    i++;
                }
                return RestResult.success(productSaleAreaRelationResponses);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }

    /*
     *  查询所有销售区域
     *
     * */
    @ResponseBody
    @GetMapping(value = "/queryAllSaleAreaRelation")
    public RestResult queryAllSaleAreaRelation() {
        RestResult restResult = new RestResult();
        List<Long> ids = new ArrayList<>();
        try {
            RestResult<List<RegionResponse>> saleAreaList = sysProvinceCityApi.getRegionListByIds(ids);

            return RestResult.success(saleAreaList.getData());
        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }


    /*
     *  销售区域保存
     *
     * */
    @ResponseBody
    @PostMapping(value = "/saveSaleArea")
    public RestResult insert(@RequestBody ProductSaleAreaEditRequest productSaleAreaEditRequest) {
        RestResult restResult = new RestResult();
        ProductSaleAreaRelation productSaleAreaRelation = null;
        try {
            if (productSaleAreaEditRequest != null) {
                for (ProductSaleAreaRelationRequest productSaleAreaRelationRequest : productSaleAreaEditRequest.getProductSaleAreaRelationRequests()) {
                    productSaleAreaRelation = BeanUtil.copy(productSaleAreaRelationRequest, ProductSaleAreaRelation.class);
                    productSaleAreaRelationService.insert(productSaleAreaRelation);
                }
                restResult.setCode("0000");
                restResult.setData("操作成功");
            } else {
                restResult.error("9999", "参数不能为空");
            }

        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }

    @ApiOperation(value = "删除商品销售区域关联")
    @ResponseBody
    @GetMapping(value = "/deleteProductSaleAreaRelation")
    public RestResult delProductSaleAreaRelation(Long saleAreaRelationId) {
        return RestResult.success(productSaleAreaRelationService.delete(saleAreaRelationId));
    }
}
